<template>
  <div class="right-panel">
    <div class="top">
      <i class="iconfont iconlianxiren1" style="font-size: 40px;color:#3300ff" />
      <i class="iconfont icontouxiang" style="font-size: 40px;color:#3300ff" />
    </div>
    <div class="content">
      <div>1</div>
      <div>2</div>
      <div>3</div>
      <div>4</div>

      <MyButton type="primary" @click="onClick">设置</MyButton>
      <el-button type="primary">主要按钮</el-button>

      <div style="display:flex">
        <div>文章标题</div>
        <input v-model="searchTitle" />

        <el-input v-model="searchTitle" />

        <el-button type="success" round @click="search">搜索</el-button>
      </div>
      <div style="display:flex">
        <el-button type="danger" @click="multiDelete" plain>删除</el-button>
        <el-button type="primary" @click="onAdd">添加</el-button>
      </div>
      <!-- 实现表格 -->
      <div class="title">
        <div class="title-item">
          <input type="checkbox" />全选
        </div>
        <div class="title-item">
          <i class="iconfont iconbianhao1" />文章ID
        </div>
        <div class="title-item">
          <i class="iconfont iconbiaoti" />文章标题
        </div>
        <div class="title-item">
          <i class="iconfont iconbiaoqian" />文章标签
        </div>
        <div class="title-item">
          <i class="iconfont iconzuozhe" />作者
        </div>
        <div class="title-item">
          <i class="iconfont iconcaozuojilu" />操作
        </div>
      </div>

      <!-- 表身 -->
      <div class="table-content" v-for="(itemData, index) in articleList" :key="itemData.id">
        <div class="title-item">
          <input type="checkbox" v-model="itemData.isCheck" />
        </div>

        <div class="title-item">{{itemData.id}}</div>
        <div class="title-item">{{itemData.title}}</div>
        <div class="title-item">{{itemData.label}}</div>
        <div class="title-item">{{itemData.author}}</div>
        <div class="title-item">
          <MyButton type="primary" size="mini" @click="edit(index)">编辑</MyButton>
          <MyButton type="danger" size="mini" @click="del(index)">删除</MyButton>
        </div>
      </div>

      <!-- 使用el-table组件 -->
      <el-table :data="articleList" style="width: 100%;margin:40px 0">
        <el-table-column type="selection" label="全选" width="180"></el-table-column>
        <el-table-column prop="id" label="文章ID" width="180"></el-table-column>
        <el-table-column prop="title" label="文章标题"></el-table-column>
        <el-table-column prop="label" label="标签"></el-table-column>
        <el-table-column prop="author" label="作者"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="edit(scope.$index)">编辑</el-button>
            <el-button size="mini" type="danger" @click="del(scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 使用el-table组件 -->
      <!-- 分页 -->
      <div
        v-for="(item, index) in totalPage"
        :key="item"
        class="page"
        @click="changePage(index)"
      >{{item}}</div>

      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        page-size="5"
        @current-change="changePage"
      ></el-pagination>

      <!-- 对话框 -->
      <transition name="fade">
        <div v-if="menuShow" class="dialog">
          <div class="dialog-title">
            <i class="iconfont iconshanchu del-icon" @click="menuShow=false" />
          </div>
          <div class="dialog-form-wrap">
            <div class="dialog-form">
              <div class="label">文章ID</div>
              <input class="ipt" v-model="id" />
            </div>
            <div class="dialog-form">
              <div class="label">文章标题</div>
              <input class="ipt" v-model="title" />
            </div>
            <div class="dialog-form">
              <div class="label">文章标签</div>
              <input class="ipt" v-model="label" />
            </div>
            <div class="dialog-form">
              <div class="label">作者</div>
              <input class="ipt" v-model="author" />
            </div>
            <div class="add-btn" @click="addNewArticle">确认</div>
          </div>
        </div>
      </transition>

      <!-- 动画组件 -->
      <Animation />
      <!-- 微博列表组件 -->
      <Blog />
    </div>
  </div>
</template>

<script>
import MyButton from "./MyButton.vue";
import MyInput from "./MyInput.vue";
import Animation from "./Animation.vue";
import Blog from "./Blog.vue";
import axios from "axios";
export default {
  name: "RightPanel",
  components: {
    MyButton,
    MyInput,
    Animation,
    Blog
  },
  created() {
    // 发起网络请求，初始请求第1页
    this.getArticle(1);
  },
  data() {
    return {
      total: 0, // 表格数据总的数量
      totalPage: 10, // 总的页数
      id: "", // 绑定文章ID输入框
      title: "", // 绑定文章标题输入框
      label: "", // 绑定文章标签输入框
      author: "", // 绑定文章作者输入框
      menuShow: false,
      isEdit: false, // 表示当前是否为编辑
      currentEditIndex: 0, // 表示当前编辑第几行
      articleList: [
        {
          id: "001",
          title: "vue开发教程",
          label: "计算机",
          author: "尤雨溪",
          isCheck: false
        },
        {
          id: "002",
          title: "三国演义",
          label: "历史",
          author: "罗贯中",
          isCheck: false
        },
        {
          id: "003",
          title: "共产党宣言",
          label: "政治",
          author: "马克思、恩格斯",
          isCheck: false
        }
      ],
      searchTitle: "" // 绑定搜索输入框的内容
    };
  },
  methods: {
    // 请求第page页表格数据的方法
    getArticle(page) {
      axios.get("/v1/test/getArticle?page=" + page).then(res => {
        console.log(res);
        // 把服务端返回的数组赋值给articleList
        this.articleList = res.data.data.list;
        this.totalPage = res.data.data.totalPage;
        this.total = res.data.data.total;
      });
    },
    onClick(param) {
      // 这里会不会触发？？
      console.log("这是父组件的回调方法", param);
    },
    onAdd() {
      // 打开对话框，也就是将menuShow改成true
      this.menuShow = true;
      this.isEdit = false; // 当前为添加状态
    },
    addNewArticle() {
      // 区分当前是添加操作还是编辑操作
      if (this.isEdit) {
        // 编辑逻辑
        // 获取4个输入框的内容，组装数据，更新到articleList数组中的第index项
        this.articleList[this.currentEditIndex] = {
          id: this.id,
          title: this.title,
          label: this.label,
          author: this.author
        };

        // 调用服务端编辑接口完成编辑操作
        // 发起网络请求
        axios
          .post("/v1/test/editArticle", {
            id: this.id,
            title: this.title,
            label: this.label,
            author: this.author
          })
          .then(res => {
            console.log(res);
            // 重新获取一下表格数据，更新表格
            this.getArticle();
          });
      } else {
        // 添加逻辑
        // 获取4个输入框的内容，组装数据，然后添加到articleList数组的末尾
        this.articleList.push({
          id: this.id,
          title: this.title,
          label: this.label,
          author: this.author
        });

        // 调用服务端添加接口完成添加操作
        // 发起网络请求
        axios
          .post("/v1/test/addArticle", {
            id: this.id,
            title: this.title,
            label: this.label,
            author: this.author
          })
          .then(res => {
            console.log(res);
            // 重新获取一下表格数据，更新表格
            this.getArticle();
          });
      }
      // 关闭对话框
      this.menuShow = false;
    },
    // 删除回调方法
    del(index) {
      // index表示要删除的是第几行
      // 删除articleList数组中的第index项数据
      // this.articleList.splice(index, 1);

      // 发起网络请求
      axios
        .post("/v1/test/deleteArticle", {
          id: this.articleList[index].id
        })
        .then(res => {
          console.log(res);
          // 重新获取一下表格数据，更新表格
          this.getArticle();
        });
    },
    // 编辑回调方法
    edit(index) {
      // 打开对话框
      this.menuShow = true;
      this.isEdit = true; // 当前为编辑状态
      this.currentEditIndex = index; // 保存当前编辑第几行
      // 把articleList数组中的第index项数据填到输入框中
      this.id = this.articleList[index].id;
      this.title = this.articleList[index].title;
      this.label = this.articleList[index].label;
      this.author = this.articleList[index].author;
    },
    // 搜索按钮 回调方法
    search() {
      // 通过filter来进行筛选
      this.articleList = this.articleList.filter(item => {
        // 判断item.title是否包含searchTitle（用户输入的搜索关键词）
        return item.title.includes(this.searchTitle);
      });
    },
    // 批量删除按钮 回调方法
    multiDelete() {
      // 通过filter来进行筛选,
      // 判断item.isCheck是否为false
      this.articleList = this.articleList.filter(item => {
        return item.isCheck == false;
      });
    },
    // 切换页数的回调方法
    changePage(index) {
      // 请求第index页的数据
      this.getArticle(index + 1);
    }
  }
};
</script>

<style scoped>
.right-panel {
  background: #f0f2f5;
  height: 100vh;
  flex: 1;
  display: flex;
  flex-direction: column;
}
.top {
  height: 80px;
  background: #fff;
}
.content {
  flex: 1;
  margin: 20px;
  background: #fff;
  padding: 20px;
  overflow: scroll;
}
.title {
  display: flex;
  background: #f2f2f2;
  border-bottom: 1px solid #eee;
  padding: 0 10px;
}
.title-item {
  flex: 1;
  height: 50px;
  line-height: 50px;
}
.iconfont {
  color: #1890ff;
  font-weight: 700;
  margin-right: 5px;
}
.table-content {
  display: flex;
  border-bottom: 1px solid #eee;
  padding: 0 10px;
}
.dialog {
  position: fixed;
  width: 400px;
  left: 50%;
  margin-left: -200px;
  height: 400px;
  top: 200px;
  border: 1px solid #ccc;
  background: #fff;
  z-index: 100;
}
.dialog-title {
  height: 50px;
  line-height: 50px;
  background: #001528;
  text-align: right;
}
.dialog-form-wrap {
  padding: 20px;
}
.dialog-form {
  display: flex;
  height: 40px;
  line-height: 40px;
  margin-bottom: 20px;
}
.label {
  width: 100px;
}
.ipt {
  width: 200px;
  outline: none;
}
.add-btn {
  width: 80px;
  height: 35px;
  line-height: 35px;
  text-align: center;
  border-radius: 5px;
  color: #fff;
  background: #1890ff;
  margin-bottom: 10px;
  cursor: pointer;
}
/* 对话框动画 */

/* 出现的时候，属性的值是哪些？ */
/* .fade-enter-active {
  opacity: 1;
  transform: scale(1);
  transition: all 10s;
} */
/* 初始状态，就是还没出现时，属性的值是哪些？ */
/* .fade-enter {
  opacity: 0;
  transform: scale(0.3);
} */
/* 准备消失的时候，属性的值是哪些 */
.fade-enter-active,
.fade-leave-active {
  opacity: 1;
  transform: scale(1);
  transition: all 0.3s;
}
/* 消失状态，属性的值是哪些 */
.fade-enter,
.fade-leave-to {
  opacity: 0;
  transform: scale(0.3);
}
/* 对话框动画 */
.page {
  display: inline-block;
  width: 30px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  background: #ccc;
  margin: 10px;
}
</style>